package com.aaa.dem1103.d02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Author: whj
 * @Version 1.0
 * @Date: 2022/11/3  16:08
 */
public class Test3 {
    public static void main(String[] args) {
        List<Student> list=new ArrayList<>();
        list.add(new Student("aaa",16));
        list.add(new Student("汪豪杰",17));
        list.add(new Student("bbb",15));
        list.add(new Student("ccc",16));
        //List<T> list,要排序的集合
        // Comparator<? super T> c： 指明排序的规则
//        Comparator<Student> comparator=new Comparator<Student>() {
//            //返回0 表示o1和o2 相同 返回大于0
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getAge()-o2.getAge();
//            }
//        };
        Comparator<Student> comparator=(o1, o2)->{
            return o2.getAge()-o1.getAge();
        };
        Collections.sort(list,comparator);
        System.out.println(list);
    }
}
class Student{
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}
